package com.app.reptile.base;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/** 
 * @Description: TODO
 * @date 2020年2月2日 上午8:45:44 
 * @ClassName: MybatisSingleton 
 */
public class MybatisSingleton {
    
	private SqlSession sqlSession;

    private SqlSessionFactory sqlSessionFactory;

    public static boolean IS = true;
	
    private static class Singleton{
        private static final MybatisSingleton instance=new MybatisSingleton();
    }
     
    private MybatisSingleton(){
    	if (!IS) {
			return;
		}
    	String resource= "mybatis-config.xml";
        try {
            //读取配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            this.setSqlSession(sqlSessionFactory.openSession(true));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
     
    public static MybatisSingleton getInstance(){
        return Singleton.instance;
    }

	public SqlSession getSqlSession() {
		return sqlSession;
	}

    public SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }

    public void setSqlSession(SqlSession sqlSession) {
		this.sqlSession = sqlSession;
	}
}
